package org.bing.flex.action {

    import hessian.client.HessianService;

    import mx.rpc.AbstractOperation;
    import mx.rpc.AsyncToken;
    import mx.rpc.IResponder;

    public class HessianAction extends GeneralAction {
        private var hessian:HessianService;

        public function HessianAction(dest:String) {
            // super(url + dest);
            var url:String=url() + dest + "$null";
            hessian=new HessianService(url);
        }

        override public function invoke(method:String, args:Array=null, onResult:Function=null, onFault:Function=null):void {
            var responder:IResponder=new HessianResponder(onResult, onFault);
            var op:AbstractOperation=hessian.getOperation(getLocalName(method));
            // var op:AbstractOperation=getOperation(getLocalName(method));
            var tk:AsyncToken;
            if (args == null) {
                tk=op.send();
            } else {
                var len:int=args.length;
                if (len == 1) {
                    tk=op.send(args[0]);
                } else if (len == 2) {
                    tk=op.send(args[0], args[1]);
                } else if (len == 3) {
                    tk=op.send(args[0], args[1], args[2]);
                } else if (len == 4) {
                    tk=op.send(args[0], args[1], args[2], args[3]);
                } else if (len == 5) {
                    tk=op.send(args[0], args[1], args[2], args[3], args[4]);
                }
            }
            tk.addResponder(responder);
        }

        private function getLocalName(name:Object):String {
            if (name is QName) {
                return QName(name).localName;
            } else {
                return String(name);
            }
        }
    }
}